<?php

namespace app\common\model;

use think\Model;

/**
 * 配置模型
 */
class Share extends Model
{

    // 表名,不含前缀
    protected $name = 'share';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;
    // 定义时间戳字段名
    protected $createTime = false;
    // 追加属性
    protected $append = [
    ];

    public function getCreatetimeAttr($value)
    {
        return date('Y-m-d H:i:s',$value);
    }

//    public function user()
//    {
//        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
//    }

    public function something()
    {
        return $this->belongsTo('Something', 'something_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * @param $user_id 用户id
     * @param $page 页面
     * @return 返回数组结果集
     */
    public function getShareAll($user_id,$page){
        try{
            $where['share.user_id']=array('eq',$user_id);
            $data=$this
                ->with(['something'])
                ->where($where) //
                ->limit(10)
                ->page($page)
                ->select();
            if(!empty($data)){
                $data = collection($data)->toArray();
            }else{
                $data=[];
            }
            $site=\think\Config::get('site');
            foreach ($data as $key=>$val){
                if($val['something']['type']==1){
                    if(!empty($site['completed_revenue'])&&$site['completed_revenue']>0){
                        if($site['completed_revenue']>100){
                            $site['completed_revenue']=100;
                        }
                        $data[$key]['something']['partake']=$val['something']['money']*(1-$site['completed_revenue']/100);

                    }else{
                        $data[$key]['something']['partake']=0;
                    }

                }elseif($val['something']['type']==2||$val['something']['type']==3){

                    if(!empty($site['share_profit'])&&$site['share_profit']>0){
                        if($site['share_profit']>100){
                            $site['share_profit']=100;
                        }
                        $data[$key]['something']['partake']=$val['something']['money']*(1-$site['share_profit']/100);

                    }else{
                        $data[$key]['something']['partake']=0;
                    }

                }

                $data[$key]['something']['imageslist']=explode(',',$val['something']['images']);
                if(!empty($val['something']['eurekaimages'])){
                    $data[$key]['something']['eurekaimageslist']=explode(',',$val['something']['eurekaimages']);
                }else{
                    $data[$key]['something']['eurekaimageslist']=[];
                }
                $data[$key]['something']['url']=url('index/detail',array('id'=>$val['something']['id']));

                if($val['something']['type']==1){
                    $data[$key]['something']['label']='失物招领';
                    $data[$key]['something']['q_name']='报酬';
                }elseif ($val['something']['type']==2){
                    $data[$key]['something']['q_name']='赏金';
                    $data[$key]['something']['label']='找人';
                }elseif ($val['something']['type']==3){
                    $data[$key]['something']['label']='找物';
                    $data[$key]['something']['q_name']='赏金';
                }

                if($val['something']['status']==1){
                    $data[$key]['something']['status_name']='进行中';
                }elseif ($val['something']['status']==2){
                    $data[$key]['something']['status_name']='确认中';
                }elseif ($val['something']['status']==3){
                    $data[$key]['something']['status_name']='已找到';
                }elseif ($val['something']['status']==4){
                    $data[$key]['something']['status_name']='已结束';
                }elseif ($val['something']['status']==5){
                    $data[$key]['something']['status_name']='已关闭';
                }elseif ($val['something']['status']==6){
                    $data[$key]['something']['status_name']='拒绝';
                }elseif ($val['something']['status']==7){
                    $data[$key]['something']['status_name']='已退款';
                }elseif ($val['something']['status']==0){
                    $data[$key]['something']['status_name']='未支付';
                }

                if($val['something']['cycle']==1){
                    $date1 =$val['something']['createtime'];
                    $data[$key]['something']['deadline']=date('Y-m-d',strtotime("$date1 +30 day"));
                }elseif ($val['something']['cycle']==2){
                    $data[$key]['something']['deadline']='永久';
                }

            }


        }catch (\Exception $e){
            return [];
        }
        return $data;

    }

    /**
     * 用于找人找物分享计算(分享计算/一级分享计算)
     * @param $receiverUserId 找到(人/物)或者失物招领用户id
     * @param $something_id 人/物/失物招领信息id
     * @return array   返回数组
     */
    public function SharingPeople($receiverUserId,$something_id){
        try{
            $something=Something::get($something_id);
            $shareModel=new Share();
            $where['something_id']=array('eq',$something_id);
            $where['parent_user_id']=array('eq',0);
            $where['is_share']=array('eq',1);
            $where['is_find']=array('eq',0);
            $where['genre']=array('eq',1);
            $shareData=$shareModel
                ->where($where)
                ->select();

            if(!empty($shareData)){
                $shareData=collection($shareData)->toArray();

                $data['count']=0;
                $data['data']=array();


                foreach ($shareData as $key=>$val){
                    if($something->publisherUserId!=$val['user_id']&&$something->receiverUserId!=$val['user_id']){
                        $data['count']++;
                        $data['data'][]=$val;
                    }
                    $UserListShare=$this->getNextShareListNew($val['user_id'],$val['something_id'],$something->publisherUserId,$something->receiverUserId,1);

                    if(!empty($UserListShare)){

                        if($data['count']>0){
                            $data['count']=count($UserListShare)+$data['count'];
                            $data['data']=array_merge($data['data'],$UserListShare);

                        }else{
                            $data['count']=count($UserListShare);
                            $data['data']=$UserListShare;

                        }

                    }
                }

            }


        }catch (\Exception $e){

            return array('count'=>0,'data'=>array());
        }

        return $data;

    }

    /**
     * 用于找人找物分享计算(分享计算)
     * @param $good_id 竞价信息id
     * @return array   返回数组
     */
    public function BidSharingPeople($receiverUserId,$good_id){
        try{
            $something=\app\common\model\AuctionGood::get($good_id);
            $shareModel=new Share();
            $where['something_id']=array('eq',$good_id);
            $where['parent_user_id']=array('eq',0);
            $where['is_share']=array('eq',1);
            $where['is_find']=array('eq',0);
            $where['genre']=array('eq',2);
            $shareData=$shareModel
                ->where($where)
                ->select();

            if(!empty($shareData)){
                $shareData=collection($shareData)->toArray();

                $data['count']=0;
                $data['data']=array();


                foreach ($shareData as $key=>$val){
                    if($something->publisherUserId!=$val['user_id']&&$something->receiverUserId!=$val['user_id']){
                        $data['count']++;
                        $data['data'][]=$val;
                    }
                    $UserListShare=$this->getNextShareListNew($val['user_id'],$val['something_id'],$something->publisherUserId,$something->receiverUserId,2);

                    if(!empty($UserListShare)){

                        if($data['count']>0){
                            $data['count']=count($UserListShare)+$data['count'];
                            $data['data']=array_merge($data['data'],$UserListShare);

                        }else{
                            $data['count']=count($UserListShare);
                            $data['data']=$UserListShare;

                        }

                    }
                }

            }


        }catch (\Exception $e){

            return array('count'=>0,'data'=>array());
        }

        return $data;

    }

    /**
     * 用于找人找物分享计算(会员裂变)
     * @param $receiverUserId 找到(人/物)或者失物招领用户id
     * @param $something_id 人/物/失物招领信息id
     * @return array   返回数组
     */
    public function VipSharingPeople($receiverUserId,$something_id){
        try{
            $something=Something::get($something_id);
            $shareModel=new Share();
            $where['something_id']=array('eq',$something_id);
            $where['parent_user_id']=array('eq',0);
            $where['is_share']=array('eq',1);
            $where['is_find']=array('eq',0);
            $where['genre']=array('eq',1);
            $shareData=$shareModel
                ->where($where)
                ->select();

            if(!empty($shareData)){
                $shareData=collection($shareData)->toArray();
                $data=array();
                foreach ($shareData as $key=>$val){
                    $data[]=$val;
                    $UserListShare=$this->getVipNextShareListNew($val['user_id'],$val['something_id'],$something->publisherUserId,$something->receiverUserId);

                    if(!empty($UserListShare)){

                        if(count($data)>0){
                            $data=array_merge($data,$UserListShare);

                        }else{
                            $data=$UserListShare;

                        }
                    }
                }

            }


        }catch (\Exception $e){

            return [];
        }

        return $data;

    }

    /**
     * 竞价分享计算(会员裂变)
     * @param $something_id 竞价信息id
     * @return array   返回数组
     */
    public function BidVipSharingPeople($receiverUserId,$something_id){
        try{
            $something=\app\common\model\AuctionGood::get($something_id);
            $shareModel=new Share();
            $where['something_id']=array('eq',$something_id);
            $where['parent_user_id']=array('eq',0);
            $where['is_share']=array('eq',1);
            $where['is_find']=array('eq',0);
            $where['genre']=array('eq',2);
            $shareData=$shareModel
                ->where($where)
                ->select();

            if(!empty($shareData)){
                $shareData=collection($shareData)->toArray();
                $data=array();
                foreach ($shareData as $key=>$val){
                    $data[]=$val;

                    $UserListShare=$this->BidgetVipNextShareListNew($val['user_id'],$val['something_id'],$something->publisherUserId,$something->receiverUserId);

                    if(!empty($UserListShare)){

                        if(count($data)>0){
                            $data=array_merge($data,$UserListShare);

                        }else{
                            $data=$UserListShare;

                        }
                    }
                }

            }


        }catch (\Exception $e){

            return [];
        }

        return $data;

    }

    /**
     * 用于会员变裂裂变查询
     * @param $parent_user_id
     * @param $something_id
     * @param $publisherUserId
     * @param $receiverUserId
     * @param array $array_list
     * @param int $i
     * @return array
     */
    public function getVipNextShareListNew($parent_user_id,$something_id,$publisherUserId,$receiverUserId,&$array_list=array(),&$i=0){
        try{
//            $where['is_share']=array('eq',1);
            $where['parent_user_id']=array('eq',$parent_user_id);
            $where['something_id']=array('eq',$something_id);
            $where['is_share']=array('eq',1);
            $where['genre']=array('eq',1);
            $data=$this
                ->where($where)
                ->field('user_id,something_id')
                ->select();
            if(!empty($data)){
                $data=collection($data)->toArray();
                $i++;
                foreach ($data as $key=>$val){
                    $array_list[]=$val;
                    if($i<30){
                        $this->getVipNextShareListNew($val['user_id'],$val['something_id'],$publisherUserId,$receiverUserId,$array_list,$i);
                    }
                }
            }else{
                return $array_list;
            }

        }catch (\Exception $e){
            return [];
        }
        return $array_list;

    }


    /**
     * 用于竞价会员变裂裂变查询
     * @param $parent_user_id
     * @param $something_id
     * @param $publisherUserId
     * @param $receiverUserId
     * @param array $array_list
     * @param int $i
     * @return array
     */
    public function BidgetVipNextShareListNew($parent_user_id,$something_id,$publisherUserId,$receiverUserId,&$array_list=array(),&$i=0){
        try{
//            $where['is_share']=array('eq',1);
            $where['parent_user_id']=array('eq',$parent_user_id);
            $where['something_id']=array('eq',$something_id);
            $where['is_share']=array('eq',1);
            $where['genre']=array('eq',2);
            $data=$this
                ->where($where)
                ->field('user_id,something_id')
                ->select();

            if(!empty($data)){
                $data=collection($data)->toArray();
                $i++;
                foreach ($data as $key=>$val){
                    $array_list[]=$val;
                    if($i<30){
                        $this->BidgetVipNextShareListNew($val['user_id'],$val['something_id'],$publisherUserId,$receiverUserId,$array_list,$i);
                    }
                }
            }else{
                return $array_list;
            }

        }catch (\Exception $e){
            return [];
        }
        return $array_list;

    }


    /**
     * 查询所有商家
     * 传入user_id就是
     * 如果是消费这user_id就是找到的用户id或者是失物招领用户id
     * @param $user_id      用户id
     * @param array $array_list 函数自己使用变量无需传
     * @return array    返回数组
     */
    public function getParentIdList($user_id,$something_id,&$array_list=array()){
        try{
            $parent_user=$this
                ->where(['user_id'=>$user_id])
                ->where(['something_id'=>$something_id])
                ->field('id,user_id,parent_user_id,total_user_id,something_id')
                ->find();
            if(!empty($parent_user)){
                $parent_user=$parent_user->toArray();
            }
            if(!empty($parent_user['parent_user_id'])){
                $share=$this
                    ->where(['user_id'=>$parent_user['parent_user_id']])
                    ->where(['something_id'=>$parent_user['something_id']])
                    ->field('id,user_id,parent_user_id,something_id,is_share,is_find,total_user_id')
                    ->find();
                if(!empty($share)){
                    $array_list[]=$share->toArray();
                    $this->getParentIdList($parent_user['parent_user_id'],$parent_user['something_id'],$array_list);
                }else{
                    return $array_list;
                }
            }else{
                return $array_list;
            }

        }catch (\Exception $e){
            return [];
        }
        return $array_list;
    }

    /**
     * @param $user_id 找到人/物或者失物招领用户id
     * @param $something_id 找人/找物/失物招领信息id
     * @return 返回一位数组
     */
    public function getOneShare($user_id,$something_id){
        try{
            $where['user_id']=array('eq',$user_id);
            $where['something_id']=array('eq',$something_id);
            $data=$this
                ->where($where)
                ->find();
            if(!empty($data)){
                $data->toArray();
            }else{
                $data=[];
            }
        }catch (\Exception $e){
            return [];
        }
        return $data;
    }

    /**
     * @param $parent_user_id   找到人/物或者失物招领用户parent_user_id
     * @param $something_id     找人/找物/失物招领信息id
     * @return                  返回一位数组
     */
    public function getParentShareData($parent_user_id,$something_id){
        try{
            $dataNew=array();
            $where['parent_user_id']=array('eq',$parent_user_id);
            $where['something_id']=array('eq',$something_id);
            $where['is_share']=array('eq',1);
            $where['is_find']=array('eq',0);
            $where['genre']=array('eq',1);
            $data=$this
                ->where($where)
                ->select();


            if(!empty($data)){
                $data=collection($data)->toArray();

                $something=Something::get($something_id);
                $something=$something->toArray();

                foreach ($data as $key=>$val){

                    if($something['type']==1){

                        if($val['user_id']!=$something['publisherUserId']&&$val['user_id']!=$something['receiverUserId']){
                            $dataNew[]=$val;
                        }
                    }else{
                        if($val['user_id']!=$something['publisherUserId']){
                            $dataNew[]=$val;
                        }
                    }

                }


            }
        }catch (\Exception $e){
            return [];
        }
        return $dataNew;
    }

    /**
     * @param $parent_user_id   竞价用户parent_user_id
     * @param $good_id     竞价信息id
     * @return                  返回一位数组
     */
    public function BidgetParentShareData($parent_user_id,$something_id){
        try{
            $dataNew=array();
            $where['parent_user_id']=array('eq',$parent_user_id);
            $where['something_id']=array('eq',$something_id);
            $where['is_share']=array('eq',1);
            $where['is_find']=array('eq',0);
            $where['genre']=array('eq',2);
            $data=$this
                ->where($where)
                ->select();


            if(!empty($data)){
                $data=collection($data)->toArray();

                $something=\app\common\model\AuctionGood::get($something_id);
                $something=$something->toArray();

                foreach ($data as $key=>$val){

                    if($val['user_id']!=$something['receiverUserId']){
                        $dataNew[]=$val;
                    }

                }


            }
        }catch (\Exception $e){
            return [];
        }
        return $dataNew;
    }

    public function getNextShareList($parent_user_id,$something_id,&$array_list=array(),&$i=0){
        try{

            $where['is_share']=array('eq',1);
            $where['is_find']=array('eq',0);
            $where['genre']=array('eq',1);
            $where['parent_user_id']=array('eq',$parent_user_id);
            $where['something_id']=array('eq',$something_id);
            $data=$this
                ->where($where)
                ->field('user_id,something_id')
                ->select();
            if(!empty($data)){
                $data=collection($data)->toArray();
                $i++;
                foreach ($data as $key=>$val){

                    $something=Something::get($something_id);
                    $something=$something->toArray();
                    if($something['type']==1){

                        if($val['user_id']!=$something['publisherUserId']&&$val['user_id']!=$something['receiverUserId']){
                            $array_list[]=$val;
                        }
                    }else{
                        if($val['user_id']!=$something['publisherUserId']){
                            $array_list[]=$val;
                        }
                    }
                    if($i<30) {
                        $this->getNextShareList($val['user_id'], $val['something_id'], $array_list, $i);
                    }

                }
//                if($data['user_id']!=$something->publisherUserId&&$data['user_id']!=$something->receiverUserId){
//                    $array_list[]=$data;
//                }


            }else{
                return $array_list;
            }

        }catch (\Exception $e){
            return [];
        }
        return $array_list;

    }
    //竞价
    public function BidgetNextShareList($parent_user_id,$something_id,&$array_list=array(),&$i=0){
        try{

            $where['is_share']=array('eq',1);
            $where['is_find']=array('eq',0);
            $where['genre']=array('eq',2);
            $where['parent_user_id']=array('eq',$parent_user_id);
            $where['something_id']=array('eq',$something_id);
            $data=$this
                ->where($where)
                ->field('user_id,something_id')
                ->select();
            if(!empty($data)){
                $data=collection($data)->toArray();
                $i++;
                foreach ($data as $key=>$val){

                    $something=\app\common\model\AuctionGood::get($something_id);
                    $something=$something->toArray();
                    if($something['type']==1){

                        if($val['user_id']!=$something['publisherUserId']&&$val['user_id']!=$something['receiverUserId']){
                            $array_list[]=$val;
                        }
                    }else{
                        if($val['user_id']!=$something['publisherUserId']){
                            $array_list[]=$val;
                        }
                    }
                    if($i<30) {
                        $this->BidgetNextShareList($val['user_id'], $val['something_id'], $array_list, $i);
                    }

                }
//                if($data['user_id']!=$something->publisherUserId&&$data['user_id']!=$something->receiverUserId){
//                    $array_list[]=$data;
//                }


            }else{
                return $array_list;
            }

        }catch (\Exception $e){
            return [];
        }
        return $array_list;

    }

    public function getNextShareListNew($parent_user_id,$something_id,$publisherUserId,$receiverUserId,$genre,&$array_list=array(),&$i=0){
        try{
//            $where['is_share']=array('eq',1);
            $where['parent_user_id']=array('eq',$parent_user_id);
            $where['something_id']=array('eq',$something_id);
            $where['is_share']=array('eq',1);
            $where['genre']=array('eq',$genre);
            $data=$this
                ->where($where)
                ->field('user_id,something_id')
                ->select();
            if(!empty($data)){
                $data=collection($data)->toArray();
                $i++;
                foreach ($data as $key=>$val){
                    if($publisherUserId!=$val['user_id']&&$receiverUserId!=$val['user_id']){
                        $array_list[]=$val;
                    }

                    if($i<30){
                        $this->getNextShareListNew($val['user_id'],$val['something_id'],$publisherUserId,$receiverUserId,$genre,$array_list,$i);
                    }
                }
            }else{
                return $array_list;
            }

        }catch (\Exception $e){
            return [];
        }
        return $array_list;

    }




}
